package cn.baimo.controller;

import cn.baimo.pojo.Division;
import cn.baimo.pojo.Doctor;
import cn.baimo.pojo.Hospital;
import cn.baimo.pojo.User;
import cn.baimo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalDoctorNumberService hospitalDoctorNumberService;
    @Autowired
    private DivisionService divisionService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private RegistrationService registrationService;

    @RequestMapping("/login")
    public String login(User user,  HttpSession session){
        System.err.println("进入：/user/login");
        User user1 = userService.selectUserByNameAndPwd(user);
        session.setAttribute("loginUser",user1);
        return "toIndexPage";
    }

    @RequestMapping("/toIndexPage")
    public String index(Model model){
        System.err.println("进入：/user/toIndexPage");
        //查询热门医院
        List<Hospital> hotHospitals1 = hospitalService.selectHotHospitals();
        List<Hospital> hotHospitals2 = hospitalService.selectHospitalsSix();
        //查询医院总数
        int hospitalTotalNum = hospitalService.selectHospitalTotalNum();
        //查询部门
        List<Division> divisions = divisionService.selectDivisions();
        //查询热门医生
        List<Doctor> hotDoctors = doctorService.selectHotDoctor();
        //查询医生总数
        int doctorTotalNum = doctorService.selectDoctorTotalNum();
        //查询服务人数
        int registrationTotalNum = registrationService.selectRegistrationTotalNum();
//        List<HospitalDoctorNumber> hospitalDoctorNumbers = hospitalDoctorNumberService.selectHospitalDoctorNumber();
//        for (Hospital hospital : hotHospitals1) {
//            for (HospitalDoctorNumber hospitalDoctorNumber : hospitalDoctorNumbers) {
//                if (hospitalDoctorNumber.getName().equals(hospital.getName())){
//                    hospital.setNum(hospitalDoctorNumber.getNum());
//                }
//            }
//        }
        model.addAttribute("hotHospitals1",hotHospitals1);
        model.addAttribute("hotHospitals2",hotHospitals2);
        model.addAttribute("hospitalTotalNum",hospitalTotalNum);
        model.addAttribute("divisions",divisions);
        model.addAttribute("hotDoctors",hotDoctors);
        model.addAttribute("doctorTotalNum",doctorTotalNum);
        model.addAttribute("registrationTotalNum",registrationTotalNum);
        return "/pages/index.jsp";
    }
}
